//
//  TKIMVideoMsgView.m
//  TKIM
//
//  Created by liupm on 15/11/3.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMVideoMsgView.h"
//#import "TKIMVideoMessage.h"
#import <MediaPlayer/MediaPlayer.h>
//#import "TKIMUIHelper.h"
//#import "TKIMFileHelper.h"
#import "TKIMLayerUtils.h"
//#import "TKIMStringHelper.h"

@interface TKIMVideoMsgView()
@property(nonatomic,strong) UIImageView *videoImageView;
@property(nonatomic,strong) TKIMVideoMessage *videoMsg;
@property(nonatomic,strong) UIButton *playButton;
@property(nonatomic,strong) UIProgressView *progressView;
@property(nonatomic,strong) TKIMLayerUtils *layerUtils;
@end

@implementation TKIMVideoMsgView

/**
 *  @Author lpm, 15-07-30 11:07:12
 *
 *  @brief  组装界面
 */
-(void)drawView
{
    [super drawView];
    [self.backgroundImageView addSubview:self.videoImageView];
    [self.videoImageView addSubview:self.playButton];
}

-(UIImageView *)videoImageView
{
    if (!_videoImageView)
    {
        _videoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, TKIMScreenWidth *0.4, 110)];
        _videoImageView.backgroundColor = [UIColor blackColor];
        _videoImageView.alpha = 0.8;
        _videoImageView.layer.cornerRadius =  13.0f;
        _videoImageView.clipsToBounds = YES;

    }
    return _videoImageView;
}

-(UIProgressView *)progressView
{
    if (!_progressView)
    {
        _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, self.videoImageView.width, 30)];
    }
    return _progressView;
}

-(UIButton *)playButton
{
    if (!_playButton)
    {
        _playButton = [[UIButton alloc] initWithFrame:CGRectMake((self.videoImageView.width-50)/2, (self.videoImageView.height-50)/2, 50, 50)];
        [_playButton setImage:TKIMGetImage(@"im_icon_playVideo") forState:UIControlStateNormal];
        [_playButton addTarget:self action:@selector(playVideo:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playButton;
}

-(TKIMLayerUtils *)layerUtils
{
    if (!_layerUtils)
    {
        UIViewController *ctl = [TKIMUIHelper getCurrentVC];
        _layerUtils = [[TKIMLayerUtils alloc] initWithContent:ctl.view];
    }
    return _layerUtils;
}


/**
 *  @Author lpm, 15-07-27 17:07:14
 *
 *  @brief  设置文字
 *
 *  @param text
 */
-(void)setMessage:(TKIMMessage *)message
{
    
    [super setMessage:message];
    
    _videoMsg = (TKIMVideoMessage *)message;
    //图片大小
    CGSize size = self.videoImageView.frame.size;
    
    
    //设置消息label的大小
    CGRect msgRect = [self getMsgFrame:message size:size];
    
    self.videoImageView.frame = msgRect;
    
    UITapGestureRecognizer *playVideo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playVideo:)];
    self.backgroundImageView.userInteractionEnabled = YES;
    [self.backgroundImageView addGestureRecognizer:playVideo];
}


/**
 *  @Author lpm, 15-10-26 17:10:01
 *
 *  @brief  播放事件
 *
 *  @param tap
 */
-(void)playVideo:(UITapGestureRecognizer *)tap
{
    if (_videoMsg.isDownloading)
    {
        return;
    }

    //如果本地没有文件则先下载
    if(![TKIMFileHelper fileIsExist:_videoMsg.localUrl fileSize:[_videoMsg.fileSize longLongValue]])
    {
        
        [self.videoImageView addSubview:self.progressView];
        
        self.videoMsg.isDownloading = YES;
        [self.layerUtils showLoading:YES withLabel:@"视频下载中"];
        
        [[TKIMManager shareInstance] fetchMessageFile:_videoMsg complete:^(NSObject *obj)
        {
            [self.layerUtils showLoading:NO];
            _videoMsg.isDownloading = NO;
        } process:^(NSInteger totalBytes, NSInteger bytesWritten) {
            
        } fail:^(NSError *error) {
            [self.layerUtils showLoading:NO];
        }];
    }
    else
    {
        NSURL *videoURL = [NSURL fileURLWithPath:_videoMsg.localUrl];
        [self play:videoURL];
    }
}


/**
 *  @Author lpm, 15-11-04 10:11:48
 *
 *  @brief  播放视频
 *
 *  @param url
 */
-(void)play:(NSURL *)url
{
    UIViewController *ctl = [TKIMUIHelper getCurrentVC];
    MPMoviePlayerViewController *moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    [moviePlayerController.moviePlayer prepareToPlay];
    moviePlayerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
    [ctl presentMoviePlayerViewControllerAnimated:moviePlayerController];
}



/**
 *  @Author lpm, 15-11-05 09:11:46
 *
 *  @brief  行高
 *
 *  @return
 */
+(CGFloat)heightForRow:(TKIMMessage *)msg
{
    //图片大小
    CGSize size = CGRectMake(0, 0, TKIMScreenWidth *0.4, 110).size;
    return   size.height + TKIMChatMsgPadding+14;
}
@end

